//app.js
App({

  doTimeStyle(str) {
    var str = str.replace(/\//g, '-')
    str = str.substring(0, str.length - 3);
    return str = str.slice(5);
  },


  onLaunch: function () {
    //wx.setStorageSync('user', "");
    var user = wx.getStorageSync('user') || {};
    this.getUser();
  },
  checkLogin:function() {
    var loginid = wx.getStorageSync('user') == "" ? -999 : wx.getStorageSync('user').UserID;
    if (loginid == -999) {
      
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return false;
    }else{
      return loginid;
    }
  },
  login:function(username,password){
    var _this = this;
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    wx.login({
      timeout: 5000,
      success: function (res) {
        if (res.code) {
          var l = 'https://api.weixin.qq.com/sns/jscode2session?appid=wxe20350c7c72055b1&secret=a397283a2b153d228d87e31e154a3cca&js_code=' + res.code + '&grant_type=authorization_code';
          wx.request({
            url: l,
            data: {},
            method: 'GET',
            success: function (res) {
              var openid = res.data.openid;
              console.log(openid)
              _this.btlogin(openid,username,password);
              wx.hideLoading();
            }
          }) 
        }
      },
      fail: function (res) {
        wx.hideLoading();
        
        wx.showToast({
          title: "登录授权超时，请稍后重试",
          icon: 'none'
        })


        //
      }
    })
  },
  btlogin: function (openid,username, password){
    var _this = this;
    var utilMd5 = require('utils/md5.js');
    var password = utilMd5.hexMD5(password); 
    wx.request({
      url: _this.globalData.apiUrl +"IS00/Login.ashx?Method=DengLu",
      data: {
        OpenId: openid,
        DengLuZhangHao:username,
        DengLuMiMa:password
      },
      method: 'GET',
      success: function (res) {
        if (res.data.state == 1) {
           var object = res.data.retData;
           object.username = username;
           object.password = password;
           wx.setStorageSync('user', object);
        }
        if (_this.userInfoReadyCallback) {
          _this.userInfoReadyCallback(res)
        }
      }
    })
  },
  getUser:function(){
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              // 可以将 res 发送给后台解码出 unionId
              wx.setStorageSync("userInfo", res.userInfo);
              // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
              // 所以此处加入 callback 以防止这种情况
              if (this.getuserInfoReadyCallback) {
                this.getuserInfoReadyCallback(res)
              }
            }
          })
        }
      }
    })
  },
  goLogin:function(){
     wx.navigateTo({
       url: '/pages/login/login',
     })
  },
  globalData: {
    userInfo: null,
    apiUrl:"https://kypt.ustcori.com:444/ISWeiXin/",
    //apiurl: 'https://kypt.ustcori.com:444/',
    // login_id: '46F6C840-16EF-427A-9873-29A39C4D115A'
  }
})